package experiment3.shudu001.models;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import experiment3.shudu001.R;

/**
 * Created by HP on 2025/4/26.
 * 偏好设置管理器
 * 负责管理应用程序的设置项和游戏统计数据的存储与读取
 * 使用SharedPreferences持久化存储以下内容：
 * 1. 界面设置（高亮显示、错误提示开关）
 * 2. 主题设置
 * 3. 游戏统计数据（总场次、最佳时间、最少错误数）
 * 4. 是否存在保存的游戏状态
 */

public class PrefsManager {
    private static final String HIGHLIGHT_KEY = "highlight";          // 高亮显示开关
    private static final String MISTAKES_KEY = "mistakes";            // 错误提示开关
    private static final String THEME_KEY = "theme";                  // 当前主题
    private static final String GAMES_PLAYED_KEY = "games_played";    // 各难度游戏次数
    private static final String BEST_TIMES_KEY = "best_times";         // 各难度最佳时间
    private static final String BEST_MISTAKES_KEY = "best_mistakes";  // 各难度最少错误数

    public static boolean getHighlightEnabled(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getBoolean(HIGHLIGHT_KEY, true);
    }

    public static void setHighlightEnabled(Context context, boolean enabled) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit().putBoolean(HIGHLIGHT_KEY, enabled).apply();
    }

    public static boolean getMistakesEnabled(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getBoolean(MISTAKES_KEY, true);
    }

    public static void setMistakesEnabled(Context context, boolean enabled) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit().putBoolean(MISTAKES_KEY, enabled).apply();
    }

    public static int getTheme(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        return prefs.getInt(THEME_KEY, R.style.AppTheme_Light);
    }

    public static void setTheme(Context context, int themeResId) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        prefs.edit().putInt(THEME_KEY, themeResId).apply();
    }

    public static boolean hasSavedGame(Context context) {
        SharedPreferences prefs = context.getSharedPreferences("SudokuPrefs", Context.MODE_PRIVATE);
        return prefs.contains("board");
    }

    public static int[] getGamesPlayed(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String[] games = prefs.getString(GAMES_PLAYED_KEY, "0,0,0").split(",");
        return new int[] {
                Integer.parseInt(games[0]),
                Integer.parseInt(games[1]),
                Integer.parseInt(games[2])
        };
    }

    public static long[] getBestTimes(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String[] times = prefs.getString(BEST_TIMES_KEY, "0,0,0").split(",");
        return new long[] {
                Long.parseLong(times[0]),
                Long.parseLong(times[1]),
                Long.parseLong(times[2])
        };
    }

    public static int[] getBestMistakes(Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String[] mistakes = prefs.getString(BEST_MISTAKES_KEY, Integer.MAX_VALUE + "," +
                Integer.MAX_VALUE + "," + Integer.MAX_VALUE).split(",");
        return new int[] {
                Integer.parseInt(mistakes[0]),
                Integer.parseInt(mistakes[1]),
                Integer.parseInt(mistakes[2])
        };
    }

    public static void updateStats(Context context, int difficulty, long time, int mistakes) {
        // 更新游戏次数
        int[] gamesPlayed = getGamesPlayed(context);
        gamesPlayed[difficulty - 1]++;

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();

        editor.putString(GAMES_PLAYED_KEY,
                gamesPlayed[0] + "," + gamesPlayed[1] + "," + gamesPlayed[2]);

        // 更新最佳时间
        long[] bestTimes = getBestTimes(context);
        if (bestTimes[difficulty - 1] == 0 || time < bestTimes[difficulty - 1]) {
            bestTimes[difficulty - 1] = time;
            editor.putString(BEST_TIMES_KEY,
                    bestTimes[0] + "," + bestTimes[1] + "," + bestTimes[2]);
        }

        // 更新最少错误
        int[] bestMistakes = getBestMistakes(context);
        if (mistakes < bestMistakes[difficulty - 1]) {
            bestMistakes[difficulty - 1] = mistakes;
            editor.putString(BEST_MISTAKES_KEY,
                    bestMistakes[0] + "," + bestMistakes[1] + "," + bestMistakes[2]);
        }

        editor.apply();
    }
}
